# use third libs
include(FetchContent)

if (DEFINED COMMON_THIRD_LIB)
  set(CEF_ROOT ${COMMON_THIRD_LIB}/cef_binary_106.1.1+g5891c70+chromium-106.0.5249.119_windows64) 
endif()
message(CEF_ROOT ====>${CEF_ROOT})

# QtCEFWidget
message("Downlaod QtCEFWidget from https://gitee.com/yunf-tech/qt-cefwidget.git")
FetchContent_Declare(
  QtCEFWidget
  GIT_REPOSITORY https://gitee.com/yunf-tech/qt-cefwidget.git
  SOURCE_DIR ${THIRD_LIB}/QtCEFWidget
  #GIT_SHALLOW    TRUE # 不拉取完整历史，相当于`git clone --depth=1`
  )
FetchContent_MakeAvailable(QtCEFWidget)

# use cef
macro(use_cef arg_project_name)
  target_compile_definitions(${arg_project_name} PRIVATE USE_LIB_CEF)
  target_include_directories(${arg_project_name} PRIVATE ${CEF_ROOT})
  target_link_directories(${arg_project_name} PRIVATE ${CEF_ROOT})
  target_link_directories(${arg_project_name} PRIVATE ${CEF_ROOT}/build/libcef_dll_wrapper)
  target_link_libraries(${arg_project_name} PRIVATE libcef_dll_wrapper libcef)

  target_include_directories(${arg_project_name} PRIVATE ${QtCEFWidget_SOURCE_DIR})
  target_link_libraries(${arg_project_name} PRIVATE QtCEFWidget)
endmacro(use_cef)

# install cef files
macro(install_cef)
  # 资源文件和目录  
  INSTALL(
    FILES 
      ${CEF_ROOT}/Resources/chrome_100_percent.pak
      ${CEF_ROOT}/Resources/chrome_200_percent.pak
      ${CEF_ROOT}/Resources/resources.pak
      ${CEF_ROOT}/Resources/icudtl.dat
    DESTINATION 
      ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>
    )
  INSTALL(DIRECTORY ${CEF_ROOT}/Resources/locales DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>)
  
  # 二进制文件和目录
  INSTALL(
    FILES 
      ${CEF_ROOT}/Release/chrome_elf.dll
      ${CEF_ROOT}/Release/libcef.dll
      ${CEF_ROOT}/Release/libEGL.dll
      ${CEF_ROOT}/Release/libGLESv2.dll
      ${CEF_ROOT}/Release/snapshot_blob.bin
      ${CEF_ROOT}/Release/v8_context_snapshot.bin
      ${CEF_ROOT}/Release/d3dcompiler_47.dll
      ${CEF_ROOT}/Release/vk_swiftshader.dll
      ${CEF_ROOT}/Release/vk_swiftshader_icd.json
      ${CEF_ROOT}/Release/vulkan-1.dll
    DESTINATION 
      ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>
    )
  #INSTALL(DIRECTORY ${CEF_ROOT}/Release/swiftshader DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIG>)
endmacro(install_cef)